package com.frost.springbook.service;

//import com.frost.springbook.dao.BookDao;
import com.frost.springbook.enums.BookStatusEnums;
import com.frost.springbook.mapper.BookInfoMapper;
import com.frost.springbook.model.BookInfo;
import com.frost.springbook.model.PageRequest;
import com.frost.springbook.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//@Component
@Slf4j
@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    public Integer insertBook(BookInfo bookInfo) {
        return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {

        // 1. 查询记录的总数
        Integer count = bookInfoMapper.count();
        // 2. 查询当前页的数据
        List<BookInfo> bookInfos = bookInfoMapper.queryListByPage(pageRequest);
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStateCN(BookStatusEnums.getDescByCode(bookInfo.getStatus()).getDesc());
        }

        return new PageResult<>(bookInfos, count, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookInfoMapper.batchDelete(ids);
    }
}
